package com.qingke.oms.command.shop;

import com.qingke.oms.command.ShopCommand;
import com.qingke.oms.pojo.GoodsRecord;
import com.qingke.oms.pojo.ItemQuantity;
import com.qingke.oms.pojo.Shop;
import com.qingke.oms.pojo.ShoppingCart;
import com.qingke.oms.pojo.ShoppingCartItem;
import com.qingke.oms.pojo.User;
import com.qingke.oms.util.OMSConsole;

public class AddToShoppingCartCommand extends ShopCommand {

	public AddToShoppingCartCommand(User user, Shop shop) {
		super(user, shop);
	}

	@Override
	public void execute() {
		int goodsId=OMSConsole.getInt("goods id:");
		if(!dao.isGoodsExistById(goodsId)){
			OMSConsole.println("The id of the goods is not existed.");
			return ;
		}
		if(!dao.isGoodsStatisticExistByShopIdAndGoodsId(shop.getId(), goodsId)){
			OMSConsole.println("There is no the id of the goods in the shop.");
			return ;
		}
		
		int quantity=OMSConsole.getInt("quantity:");
		if(quantity==0){
			OMSConsole.println("cancel add goods to shopping cart.");
			return ;
		}
		GoodsRecord goodsRecord=dao.selectGoodsStatisticByShopIdAndGoodsId(shop.getId(),goodsId);
		if(quantity>goodsRecord.getQuantity()){
			OMSConsole.println("The input is more than the stock of the goods.");
			OMSConsole.println("Default the input equal the stock of the goods.");
			OMSConsole.println("[new input]:"+goodsRecord.getQuantity());
			quantity=goodsRecord.getQuantity();
		}
		
		if(!dao.isShoppingCartExistByCustomerIdAndShopId(user.getId(),shop.getId())){
			boolean isSuccess=dao.insertShoppingCart(user.getId(),shop.getId());
			if(!isSuccess){
				OMSConsole.println("create your shopping cart failed.");
				return ;
			}
		}
		
		ShoppingCart shoppingCart=dao.selectShoppingCart(user.getId(),shop.getId());
		boolean isShoppingCartItemExist=dao.isShoppingCartItemExistByShoppingCartIdAndGoodsId(shoppingCart.getId(), goodsId);
		if(!isShoppingCartItemExist){
			boolean isSuccess=dao.insertShoppingCartItem(shoppingCart.getId(), goodsRecord.getGoods().getId(), goodsRecord.getPrice());
			if(!isSuccess){
				OMSConsole.println("create shopping cart item failed.");
				return ;
			}
		}
		ShoppingCartItem shoppingCartItem=dao.selectShoppingCartItemByShoppingCartIdAndGoodsId(shoppingCart.getId(), goodsRecord.getGoods().getId());
		ItemQuantity itemQuantity=dao.selectItemQuantityByShoppingCartItemId(shoppingCartItem.getId());
		int oldQuantity=0;
		if(itemQuantity!=null){
			oldQuantity=itemQuantity.getQuantity();
		}
		if(oldQuantity+quantity>goodsRecord.getQuantity()){
			OMSConsole.println("add too much");
			return ;
		}
		boolean isSuccess=dao.insertItemQuantity(shoppingCartItem.getId(),
				oldQuantity+quantity);
		
		if(!isSuccess){
			OMSConsole.println("create item quantity failed.");
			return ;
		}
		boolean isSuccessUpdate=dao.updateShoppingCart(shoppingCart.getTotal()+shoppingCartItem.getPrice()*quantity, user.getId(), shop.getId());
		if(!isSuccessUpdate){
			OMSConsole.println("update shopping cart total failed.");
			OMSConsole.println("The total need be careful.");
			return ;
		}
		OMSConsole.println("Add success.");
		
	}

}
